package calculation

import (
	"cloudDevice2/sampleapi/protobuf"
	"cloudDevice2/sampleapi/utils"

	"fmt"
	"log"

	"github.com/golang/protobuf/proto"
)

func PrepareRriData(pbData []byte) {
	hisNotify := &protobuf.HisNotification{}
	err := proto.Unmarshal(pbData, hisNotify)
	if err != nil {
		log.Println(fmt.Sprintf("parse 80 healh history error:%s", err.Error()))
		return
	}

	switch hisNotify.GetData().(type) {
	case (*protobuf.HisNotification_HisData):
		hisData := hisNotify.GetHisData()
		switch hisNotify.GetType() {
		case protobuf.HisDataType(protobuf.HisDataType_RRI_DATA):
			/*
			   not like ecg, rri is not one time measure, but
			   continuous value, you can calculate af result
			   of random time range, combine all rri value of
			   the time range for calculation
			*/
			rriData := hisData.GetRri()
			unix_timestamp := rriData.GetTimeStamp().GetDateTime().GetSeconds()
			time_str := utils.ParsePbDateTime(int64(unix_timestamp))
			rawDataList := rriData.GetRawData()
			rri_list := make([]int, 0)
			for _, rawData := range rawDataList {
				var value int64 = int64(rawData)
				var fValue int64 = (value >> 16) & 0x0000ffff
				var sValue int64 = value & 0x0000ffff
				rri_list = append(rri_list, int(int16(fValue)))
				rri_list = append(rri_list, int(int16(sValue)))
			}
			log.Println(fmt.Sprintf("%s rri count:%d", time_str, len(rri_list)))
		}
	}
}
